package com.ruoyi.system.module.log;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.SysOperLog;
import com.ruoyi.system.domain.YhLogDisplayConversion;
import com.ruoyi.system.service.ISysOperLogService;
import com.ruoyi.system.service.IYhLogDisplayConversionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作日志记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/dr/operlog")
public class SysDrOperlogController extends BaseController
{
    @Autowired
    private ISysOperLogService operLogService;
    @Autowired
    private IYhLogDisplayConversionService yhLogDisplayConversionService;

//    @RequiresPermissions("system:operlog:list")
    @GetMapping("/list")
    public TableDataInfo list(SysOperLog operLog)
    {
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        List<SysOperLog> list2 = new ArrayList<SysOperLog>();

        YhLogDisplayConversion yhLogDisplayConversion = new YhLogDisplayConversion();
        yhLogDisplayConversion.setDisplay("0");
        List<YhLogDisplayConversion> list1 = yhLogDisplayConversionService.selectYhLogDisplayConversionList(yhLogDisplayConversion);
        Map<String, String> m1 = new HashMap<String, String>();
        for(YhLogDisplayConversion display : list1){
            System.out.println(display.getFieldLabels());
            m1.put(display.getFieldLabels(), display.getFieldName());
        }

        // 遍历替换
        for(SysOperLog log : list){
            JSONObject info = new JSONObject();
            String param = log.getOperParam();
            System.out.println("param" + param);
            JSONObject obj = JSONObject.parseObject(param);
            // 每个里面的jsonobject
            for (Map.Entry<String, Object> stringObjectEntry : obj.entrySet()) {
                String key = stringObjectEntry.getKey();
                Object value = stringObjectEntry.getValue();
                /*System.out.println(key+"----"+value);*/
                // 查询并转化 key
                key = m1.get(key);
                System.out.println("key:"+key);
                // 转换完，插入
                info.put(key,value);
            }
            System.out.println("输出结果：" + info.toString());
            log.setOperParam(info.toString());
            list2.add(log);
        }

        return getDataTable(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("system:operlog:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog)
    {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
//    @RequiresPermissions("system:operlog:remove")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

//    @RequiresPermissions("system:operlog:remove")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        operLogService.cleanOperLog();
        return success();
    }

    @InnerAuth
    @PostMapping
    public AjaxResult add(@RequestBody SysOperLog operLog)
    {
        return toAjax(operLogService.insertOperlog(operLog));
    }
}
